<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="上传文件">
				<el-upload
					ref="upload"
					class="upload-demo"
					:action="constant.uploadUrl"
					:before-upload="beforeUpload"
					:on-success="handleSuccess"
					accept=".pdf"
					multiple
					:on-change="handleChangePic"
				>
					<template #trigger>
						<el-button type="primary">选择文件</el-button>
						<span style="color: red; font-size: 12px; margin-left: 10px">*仅支持上传pdf文件</span>
					</template>
				</el-upload>
			</el-form-item>
			<el-form-item label="名称" prop="name">
				<el-input v-model="dataForm.name"></el-input>
			</el-form-item>
			<el-form-item label="标准号" prop="standardNo">
				<el-input v-model="dataForm.standardNo"></el-input>
			</el-form-item>
			<el-form-item label="标准分类" prop="standardGroup">
				<fast-select v-model="dataForm.standardGroup" style="width: 100%" dict-type="file_standard_type" placeholder="请选择" clearable></fast-select>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-select style="width: 100%" v-model="dataForm.status">
					<el-option :value="'1'" label="现行"></el-option>
					<el-option :value="'0'" label="废止"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="发布日期" prop="issuingDate">
				<el-date-picker type="date" placeholder="发布日期" v-model="dataForm.issuingDate"></el-date-picker>
			</el-form-item>
			<el-form-item label="实施日期" prop="implementationDate">
				<el-date-picker type="date" placeholder="实施日期" v-model="dataForm.implementationDate"></el-date-picker>
			</el-form-item>
			<el-form-item label="所属分组" prop="path">
				<el-input disabled v-model="dataForm.path" placeholder="所属分组"></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="description">
				<el-input :rows="2" type="textarea" v-model="dataForm.description" placeholder="备注"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" :loading="loading" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import constant from '@/utils/constant'
import { useFileApi, useFileSubmitApi } from '@/api/data-integrate/file'
import type { UploadInstance, UploadProps } from 'element-plus'
import timeFormater from 'time-formater'

const emit = defineEmits(['refreshDataList'])

const upload = ref<UploadInstance>()
const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	name: '',
	fileCategoryId: '',
	path: '',
	type: '',
	fileUrl: '',
	size: '',
	description: '',
	projectId: '',
	standardNo: '',
	standardGroup: '',
	status: '',
	issuingDate: '',
	implementationDate: ''
})

const loading = ref(false)

const init = (id?: number, fileCategoryId: number, path: any, projectId: any) => {
	loading.value = false
	visible.value = true
	dataForm.id = ''
	if (upload.value) {
		upload.value.clearFiles()
	}

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getFile(id)
	}

	dataForm.fileCategoryId = fileCategoryId
	dataForm.path = path
	dataForm.projectId = projectId
}

const getFile = (id: number) => {
	useFileApi(id).then(res => {
		Object.assign(dataForm, res.data)
		dataForm.fileUrl = ''
		dataForm.name = ''
	})
}

const dataRules = ref({
	name: [{ required: true, pattern: '[^ \x22]+', message: '必填项不能为空', trigger: 'blur' }],
	standardNo: [{ required: true, pattern: '[^ \x22]+', message: '必填项不能为空', trigger: 'blur' }],
	standardGroup: [{ required: true, pattern: '[^ \x22]+', message: '必填项不能为空', trigger: 'change' }],
	status: [{ required: true, message: '必填项不能为空', trigger: 'change' }]
})

const beforeUpload: UploadProps['beforeUpload'] = file => {
	if (file.size / 1024 / 1024 / 1024 / 1024 > 1) {
		ElMessage.error('文件大小不能超过100M')
		return false
	}

	if (file.type !== 'application/pdf') {
		ElMessage.error('上传文件只能是pdf格式')
		return false
	}
	return true
}

const handleChangePic = (file: any, fileList: any) => {
	if (fileList.length > 1) {
		fileList.splice(0, 1)
	}
}
/**
 * 上传成功触发
 */
const handleSuccess: UploadProps['onSuccess'] = (res, file) => {
	if (res.code !== 0) {
		ElMessage.error('上传失败：' + res.message)
		return false
	}

	dataForm.name = res.data.name
	dataForm.type = res.data.suffix
	dataForm.fileUrl = res.data.url
	dataForm.size = res.data.size
}

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			loading.value = false
			return false
		}
		if (!dataForm.fileUrl) {
			ElMessage.warning({
				message: '请选择文件上传后再提交！'
			})

			loading.value = false
			return
		}

		if (dataForm.issuingDate) {
			dataForm.issuingDate = timeFormater(dataForm.issuingDate).format('YYYY-MM-DD HH:mm:ss')
		}
		if (dataForm.implementationDate) {
			dataForm.implementationDate = timeFormater(dataForm.implementationDate).format('YYYY-MM-DD HH:mm:ss')
		}

		loading.value = true
		useFileSubmitApi(dataForm)
			.then(() => {
				ElMessage.success({
					message: '操作成功',
					duration: 500,
					onClose: () => {
						loading.value = false
						visible.value = false
						emit('refreshDataList')
					}
				})
			})
			.catch(() => {
				loading.value = false
			})
	})
}

defineExpose({
	init
})
</script>
